package com.hariro.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.qianfeng.project.R;

public class StyleTestActivity_GirlLike extends Activity {
	// private String baseUrl =
	// "http://www.j.cn/m/pickxks_app2?jcnsource=android&jcnappid=000000000000000";
	// http://www.j.cn/skin/skin_color?jcnsource=android&jcnappid=868033010199109肤色测试
	// http://depart.femh.org.tw/dietary/3OPD/BMI.htm // BMI测试
	// http://android.myapp.com/myapp/union.htm?orgame=1&typeId=16&page=1
	// //女生喜欢的app
	private String baseUrl = "http://android.myapp.com/myapp/union.htm?orgame=1&typeId=16&page=1";
	/** Called when the activity is first created. */
	private WebView wv;
	private ProgressDialog pd;
	private Handler handler;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_web_to_buy);

		init();// 执行初始化函数
		// Intent intent = getIntent();
		// String buyUrl = intent.getStringExtra("buyUrl");
		// Toast.makeText(this, buyUrl, 1).show();
		loadurl(wv, baseUrl);
		handler = new Handler() {
			public void handleMessage(Message msg) {// 定义一个Handler，用于处理下载线程与UI间通讯
				if (!Thread.currentThread().isInterrupted()) {
					switch (msg.what) {
					case 0:
						pd.show();// 显示进度对话框
						break;
					case 1:
						pd.hide();// 隐藏进度对话框，不可使用dismiss()、cancel(),否则再次调用show()时，显示的对话框小圆圈不会动。
						break;
					}
				}
				super.handleMessage(msg);
			}
		};
	}

	public void clickButton(View view) {
		switch (view.getId()) {
		case R.id.imageButton_comeback:
			finish();
			break;
		case R.id.imageView_common_refresh:
			Intent intent = getIntent();
			String buyUrl = intent.getStringExtra("buyUrl");
			loadurl(wv, buyUrl);
			break;
		default:
			break;
		}
	}

	public void init() {// 初始化
		wv = (WebView) findViewById(R.id.webView_toBuy);
		wv.getSettings().setJavaScriptEnabled(true);// 可用JS
		wv.setScrollBarStyle(0);// 滚动条风格，为0就是不给滚动条留空间，滚动条覆盖在网页上
		wv.setWebViewClient(new WebViewClient() {
			public boolean shouldOverrideUrlLoading(final WebView view,
					final String url) {
				loadurl(view, url);// 载入网页
				return true;
			}// 重写点击动作,用webview载入

		});
		wv.setWebChromeClient(new WebChromeClient() {
			public void onProgressChanged(WebView view, int progress) {// 载入进度改变而触发
				if (progress == 100) {
					handler.sendEmptyMessage(1);// 如果全部载入,隐藏进度对话框
				}
				super.onProgressChanged(view, progress);
			}
		});

		pd = new ProgressDialog(StyleTestActivity_GirlLike.this);
		pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		pd.setMessage("数据载入中，请稍候！");
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {// 捕捉返回键
		if ((keyCode == KeyEvent.KEYCODE_BACK) && wv.canGoBack()) {
			wv.goBack();
			return true;
		} else if (keyCode == KeyEvent.KEYCODE_BACK) {
			ConfirmExit();// 按了返回键，但已经不能返回，则执行退出确认
			// finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	public void ConfirmExit() {// 退出确认
		AlertDialog.Builder ad = new AlertDialog.Builder(
				StyleTestActivity_GirlLike.this);
		ad.setTitle("退出");
		ad.setMessage("是否退出软件?");
		ad.setPositiveButton("是", new DialogInterface.OnClickListener() {// 退出按钮
					@Override
					public void onClick(DialogInterface dialog, int i) {
						// TODO Auto-generated method stub
						StyleTestActivity_GirlLike.this.finish();// 关闭activity

					}
				});
		ad.setNegativeButton("否", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int i) {
				// 不退出不用执行任何操作
			}
		});
		ad.show();// 显示对话框
	}

	public void loadurl(final WebView view, final String url) {
		new Thread() {
			public void run() {
				handler.sendEmptyMessage(0);
				view.loadUrl(url);// 载入网页
			}
		}.start();
	}
}
